package com.example.demo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.demo.ChatActivity;
import com.example.demo.ContactActivity2;
import com.example.demo.R;
import com.example.demo.adapter.ChatContactAdapter;
import com.example.demo.adapter.GameAdapter;
import com.example.demo.entity.ChatContact;
import com.example.demo.entity.Game;

import java.util.ArrayList;
import java.util.List;

public class GameFragment extends Fragment {

    private GameAdapter adapter;
    private RecyclerView recyclerView;
    private List<Game> gameList = new ArrayList<>();
    private Context context;
    private SwipeRefreshLayout refreshLayout;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_game, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = getContext();

        recyclerView = view.findViewById(R.id.recycler_view);
        refreshLayout = view.findViewById(R.id.refreshLayout);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                if (refreshLayout.isRefreshing()){
                    refreshLayout.setRefreshing(false);
                }
            }
        });

        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);

        adapter = new GameAdapter();
        recyclerView.setAdapter(adapter);

        initData();
        adapter.setData(gameList);
    }

    private void initData(){
        for (int i = 0; i < 5; i++){
            Game game = new Game();
            game.setInstall("安装");
            game.setRank(String.valueOf(i + 1));
            game.setSize("1.98GB");
            game.setName("流浪地球");
            game.setCount("30亿次下载");
            game.setImg(R.drawable.a5);

//            if (i % 2 == 0){
//                game.setSize("1.98GB");
//                game.setName("流浪地球");
//                game.setCount("30亿次下载");
//                game.setImg(R.drawable.a5);
//            }
//            else {
//                game.setSize("2.98GB");
//                game.setName("三体：黑暗森林");
//                game.setCount("10亿次下载");
//                game.setImg(R.drawable.a3);
//             }
            gameList.add(game);
        }
    }

}
